#!/bin/bash

cd $(cd `dirname $0`; pwd)
sudo rm data_deliver

export MODE=${MODE:-"Debug"}
export PLATFORM=${PLATFORM:-"x64"}
export REDIRECT=${REDIRECT:-"to_term"}

if [ "$MODE" == "Release" ];then
    CFLAG="RELEASE"
else
    CFLAG="DEBUG"
    MODE="Debug"
fi

if [ "$PLATFORM" == "x86" ];then
    APPABI="-m32"
else
    PLATFORM="x64"
    APPABI=""
fi

if [ "$REDIRECT" == "to_file" ];then
    LOG_DIRECTION="TO_FILE"
else
    REDIRECT="to_term"
    LOG_DIRECTION="TO_TERMINAL"
fi

#echo "MODE     : Debug   / Release (default Debug)"
#echo "PLATFORM : x64     / x86 (default x64)"
#echo "REDIRECT : to_term / to_file (default to_file)"
#echo -e "\n"

#echo -e "\033[32m--------------------------------------------------------- \033[0m"
#echo -e "\033[32m compile begein! \033[0m"
#echo "MODE     : $MODE"
#echo "PLATFORM : $PLATFORM"
#echo "REDIRECT : $REDIRECT"

./version_info/version_gen.sh

if [ $? -eq 0 ]; then
    echo -e "\033[32m version_gen successful! \033[0m"
else
    echo -e "\033[31m -----------version_gen.sh failed!------------- \033[0m"
fi
gcc $APPABI -I ./ -I ./pthread_lib -I ./version_info -o data_deliver data_deliver.c low_speed_port.c cmd.c data_interface.c parse_para.c usr_xdma.c ./pthread_lib/pthread_own.c ./version_info/version.c -lpthread  -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
#gcc  $APPABI -I ./ -I ./pthread_lib -I ./version_info -o ./make_data/make_data ./make_data/make_data.c ./pthread_lib/pthread_own.c ./version_info/version.c -lpthread -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION

gcc  $APPABI -I ./ -I ./pthread_lib data_handle_lib.c ./pthread_lib/pthread_own.c -fPIC -shared -o libdata_handle_lib.so -lpthread -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
cp libdata_handle_lib.so /usr/lib
gcc  $APPABI -I ./ -I pthread_lib -o test_debug_named_pipe test_debug_named_pipe.c -lpthread -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
gcc $APPABI -L ./ -I pthread_lib -o read_write_alt read_write_alt.c -lpthread -ldata_handle_lib -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
gcc $APPABI -L ./ -I ./pthread_lib -o update_z7_firmware update_z7_firmware.c md5sum.c -lpthread -ldata_handle_lib -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
gcc $APPABI -L ./ -I ./pthread_lib -o send_file send_file.c md5sum.c -lpthread -ldata_handle_lib -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION
#gcc  $APPABI -o syc_422_make_data syc_422_make_data.c -lpthread -ldata_handle_lib -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -D $CFLAG -D $LOG_DIRECTION

if [ $? -eq 0 ]; then
    echo -e "\033[32m Successfully build! \033[0m"
else
    echo -e "\033[31m -----------build failed!------------- \033[0m"
fi
echo -e "\033[32m--------------------------------------------------------- \033[0m"
